<template>
    <div>
        <!-- <div class="switch-language">
            <el-dropdown size="large" :hide-timeout="50" placement="bottom-end" :hide-on-click="true">
                <Icon name="fa fa-globe" color="var(--color-secondary)" size="28" />
                <template #dropdown>
                    <el-dropdown-menu class="chang-lang">
                        <el-dropdown-item v-for="item in config.lang.langArray" :key="item.name" @click="editDefaultLang(item.name)">
                            {{ item.value }}
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div> -->
        <div class="bg-mask"></div>
        <div @contextmenu.stop="" id="bubble" class="bubble">
            <canvas id="bubble-canvas" class="bubble-canvas"></canvas>
        </div>
        <div class="login">
            <div class="login-box">
                <div class="head">
                    <img src="~assets/login-header.png" alt="" />
                </div>
                <div class="form">
                    <img class="profile-avatar" src="~assets/avatar.png" alt="" />
                    <div class="content">
                        <el-form @keyup.enter="onSubmit(formRef)" ref="formRef" :rules="rules" size="large" :model="form.data">
                            <el-form-item prop="account">
                                <el-input
                                    ref="usernameRef"
                                    type="text"
                                    clearable
                                    v-model="form.data.account"
                                    placeholder="请输入账号"
                                >
                                    <template #prefix>
                                        <Icon name="fa fa-user" class="form-item-icon" size="16" color="var(--el-input-icon-color)" />
                                    </template>
                                </el-input>
                            </el-form-item>
                            <el-form-item prop="password">
                                <el-input
                                    ref="passwordRef"
                                    v-model="form.data.password"
                                    type="password"
                                    placeholder="请输入密码"
                                    show-password
                                >
                                    <template #prefix>
                                        <Icon name="fa fa-unlock-alt" class="form-item-icon" size="16" color="var(--el-input-icon-color)" />
                                    </template>
                                </el-input>
                            </el-form-item>
                            <el-form-item prop="captcha">
                                <el-row class="w100" :gutter="15">
                                    <el-col :span="16">
                                        <el-input
                                            ref="captchaRef"
                                            type="text"
                                            placeholder="请输入验证码"
                                            v-model="form.data.code"
                                            clearable
                                            autocomplete="off"
                                        >
                                            <template #prefix>
                                                <Icon name="fa fa-ellipsis-h" class="form-item-icon" size="16" color="var(--el-input-icon-color)" />
                                            </template>
                                        </el-input>
                                    </el-col>
                                    <el-col :span="8">
                                        <div
                                            @click="onChangeCaptcha"
                                            class="captcha-img"
                                            v-html="form.captcha"
                                        ></div>
                                    </el-col>
                                </el-row>
                            </el-form-item>
                            <el-checkbox v-model="keep" label="记住密码" size="default"></el-checkbox>
                            <el-form-item>
                                <el-button :loading="form.loading" class="submit-button" round type="primary" size="large" @click="onSubmit(formRef)">
                                    登录
                                </el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount, reactive, ref } from 'vue'
import * as pageBubble from '/@/utils/pageBubble'
import type { ElForm, ElInput } from 'element-plus'
import { ElNotification } from 'element-plus'
import { useAdminInfo } from '/@/stores/adminInfo'
// import { editDefaultLang } from '/@/lang/index'
// import { useConfig } from '/@/stores/config'
import { login,captcha,getInfo } from '/@/api/admin'
import router from '/@/router'
import { Local } from '/@/utils/storage'
var timer: NodeJS.Timer

// const config = useConfig()
const adminInfo = useAdminInfo()

const onChangeCaptcha = async () => {
    const {data} = await captcha()
    form.captcha = data.captcherImg
    form.data.userKey = data.userKey
}

const formRef = ref<InstanceType<typeof ElForm>>()
const usernameRef = ref<InstanceType<typeof ElInput>>()
const passwordRef = ref<InstanceType<typeof ElInput>>()
const captchaRef = ref<InstanceType<typeof ElInput>>()
const form = reactive({
    loading: false,
    captcha:'',
    data:{
        account: '',
        code: '',
        password: '',
        userKey: ''
    }
})

const keep = ref(false)
const getKeep = () => {
    if(Local.get('keep')==='1'){
        const keepData = Local.get('keepData')
        keep.value = true
        form.data.account = keepData.account
        form.data.password = keepData.password
    }else{
        keep.value = false
    }
}

// 表单验证规则
const rules = reactive({
    account: [
        {
            required: true,
            message: '请输入账号',
            trigger: 'blur',
        }
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
        }
    ],
    code: [
        {
            required: true,
            message: '请输入验证码',
            trigger: 'blur',
        }
    ],
})

onMounted(async () => {
    timer = setTimeout(() => {
        pageBubble.init()
    }, 1000)
    onChangeCaptcha()
    getKeep()
})

onBeforeUnmount(() => {
    clearTimeout(timer)
    pageBubble.removeListeners()
})

const onSubmit = (formEl: InstanceType<typeof ElForm> | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            form.loading = true
            try{
                const {data} = await login(form.data)
                Local.set("token",data.token)
                const {data:adminInfoRes} = await getInfo()
                adminInfo.$state = adminInfoRes
                form.loading = false
                ElNotification({
                    message: '登录成功',
                    type: 'success'
                })
                if(keep.value){
                    Local.set('keep','1')
                    Local.set('keepData',{
                        account:form.data.account,
                        password:form.data.password
                    })
                }else{
                    Local.remove('keep')
                    Local.remove('keepData')
                }
                router.push({ path: '/' })
            }catch(e){
                onChangeCaptcha()
                form.loading = false
            }
        } else {
            onChangeCaptcha()
            return false
        }
    })
}
</script>

<style scoped lang="scss">
.switch-language {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 1;
}
.bg-mask{
    position:fixed;
    left:0;
    top:0;
    width:100%;
    height:100%;
    background:rgba(0,0,0,.5);
}
.bubble {
    overflow: hidden;
    background-image: url(/@/assets/bg.png);
    background-size:cover;
    background-repeat:no-repeat;
}
.form-item-icon {
    height: auto;
}
.login {
    position: absolute;
    top: 0;
    display: flex;
    width: 100vw;
    height: 100vh;
    align-items: flex-start;
    justify-content: center;
    .login-box {
        overflow: hidden;
        width: 430px;
        padding: 0;
        background: #fff;
        margin-top: 130px;
    }
    .head {
        background: #ccccff;
        img {
            display: block;
            width: 430px;
            margin: 0 auto;
            user-select: none;
        }
    }
    .form {
        position: relative;
        .profile-avatar {
            display: block;
            position: absolute;
            height: 100px;
            width: 100px;
            border-radius: 50%;
            border: 4px solid #fff;
            top: -50px;
            left: 50%;
            margin-left:-50px;
            z-index: 2;
            user-select: none;
        }
        .content {
            padding: 100px 40px 40px 40px;
        }
        .submit-button {
            width: 100%;
            letter-spacing: 2px;
            font-weight: 300;
            margin-top: 15px;
        }
    }
}

@media screen and (max-width: 720px) {
    .login {
        display: flex;
        align-items: center;
        justify-content: center;
        .login-box {
            width: 340px;
            margin-top: 0;
        }
    }
}
.chang-lang :deep(.el-dropdown-menu__item) {
    justify-content: center;
}
.content :deep(.el-input__prefix) {
    display: flex;
    align-items: center;
}
.captcha-img {
    cursor: pointer;
    width: 100%;
    height: 40px;
}
</style>
